package com.wc.codeforces.思维.Rectangle_Filling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/6/12 22:00
 * @description https://codeforces.com/problemset/problem/1966/B
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510;
    static char[][] g = new char[N][];
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            for (int i = 1; i <= n; i++) {
                g[i] = (" " + sc.next()).toCharArray();
            }
            boolean res = false;
            if (n == 1) {
                res = g[1][1] == g[1][m];
            } else if (m == 1) {
                res = g[1][1] == g[n][1];
            } else {
                // 0 ~ 3 ,  上下左右
                boolean[] W = new boolean[4];
                boolean[] B = new boolean[4];
                for (int i = 1; i <= m; i++) {
                    if (g[1][i] == 'W') W[0] = true;
                    else B[0] = true;
                    if (g[n][i] == 'W') W[1] = true;
                    else B[1] = true;
                }
                for (int i = 1; i <= n; i++) {
                    if (g[i][1] == 'W') W[2] = true;
                    else B[2] = true;
                    if (g[i][m] == 'W') W[3] = true;
                    else B[3] = true;
                }
                if (W[0] && W[1] && W[2] && W[3]) res = true;
                if (B[0] && B[1] && B[2] && B[3]) res = true;
            }
            out.println(res ? "YES" : "NO");
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
